<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>数据</el-breadcrumb-item>
        <el-breadcrumb-item>产品</el-breadcrumb-item>
        <el-breadcrumb-item>新增阅读情况</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
      >
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { productData as baseURL, getGroupList } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
        // pageSizes:[1,2,3]
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "statistics_date&DESC,app_version&DESC,total_dau_num&DESC",
        type: 1,
      },
      option: {
        index: true,
        indexLabel: "编号",
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: true,
        selection: true,
        border: true,
        menu: false,
        column: [
          {
            label: "日期",
            prop: "statistics_date",
            type: "daterange",
            search: true,
          },
          {
            label: "版本",
            prop: "app_version",
            type: "input",
            search: true,
          },
          {
            label: "渠道号",
            prop: "channel_id",
            type: "input",
            search: true,
          },
          {
            label: "用户群组",
            prop: "group_id",
            type: "select",
            hide: true,
            search: true,
            dicUrl: `${catUrl}${getGroupList.getGroupList}`,
            dicFormatter: (res) => {
              return res.source;
            },
            props: {
              label: "group_name",
              value: "group_id",
            },
            editDisplay: false,
          },
          {
            label: "新增用户数",
            prop: "total_dau_num",
            type: "input",
          },
          {
            label: "阅读用户数",
            prop: "total_user_num",
            type: "input",
          },
          {
            label: "阅读率",
            prop: "reading_rate",
            type: "number",
          },
          {
            label: "图书被读数(图书id去重)",
            prop: "read_book_num",
            type: "input",
          },
          {
            label: "章节被读数(章节id去重)",
            prop: "read_chapter_num",
            type: "input",
          },
          {
            label: "阅读图书数(按人去重图书id)",
            prop: "total_book_num",
            type: "input",
          },
          {
            label: "阅读章节数(按人去重章节id)",
            prop: "total_chapter_num",
            type: "input",
          },
          {
            label: "总阅读时长(分钟)",
            prop: "total_consume_time",
            type: "input",
          },
          {
            label: "人均阅读章节数",
            prop: "per_chapter_num",
            type: "input",
          },
          {
            label: "人均阅读时长(分钟)",
            prop: "per_consume_time",
            type: "input",
          },
        ],
      },
    };
  },
  methods: {
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.getReadActionChannelListByType,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      let totalDau = 0;
      let totalUser = 0;
      let readingTotal = 0;
      let readBookTotal = 0;
      let readChapterTotal = 0;
      let totalBook = 0;
      let totalChapter = 0;
      let totalConsume = 0;
      let perChapterNum = 0;
      let perConsumeTime = 0;
      // total_book_num

      // read_chapter_num
      // read_book_num
      let len = response.data.length - 1;
      response.data.forEach((element) => {
        totalDau += element.total_dau_num;
        totalUser += element.total_user_num;
        readingTotal += element.reading_rate;
        readBookTotal += element.read_book_num;
        readChapterTotal += element.read_chapter_num;
        totalBook += element.total_book_num;
        totalChapter += element.total_chapter_num;
        totalConsume += element.total_consume_time;
        perChapterNum += element.per_chapter_num;
        perConsumeTime += element.per_consume_time;
      });
      let item = {
        statistics_date: "合计/平均",
        total_dau_num: totalDau + " / " + (totalDau / len).toFixed(2),
        total_user_num: totalUser + " / " + (totalUser / len).toFixed(2),
        reading_rate: readingTotal + " / " + (readingTotal / len).toFixed(2),
        read_book_num: readBookTotal + " / " + (readBookTotal / len).toFixed(2),
        read_chapter_num:
          readChapterTotal + " / " + (readChapterTotal / len).toFixed(2),
        total_book_num: totalBook + " / " + (totalBook / len).toFixed(2),
        total_chapter_num:
          totalChapter + " / " + (totalChapter / len).toFixed(2),
        total_consume_time:
          totalConsume + " / " + (totalConsume / len).toFixed(2),
        per_chapter_num:
          perChapterNum + " / " + (perChapterNum / len).toFixed(2),
        per_consume_time:
          perConsumeTime + " / " + (perConsumeTime / len).toFixed(2),
      };
      response.data.unshift(item);
      this.data = response.data;
      this.loading = false;
    } /**刷新 */,
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      params.statistics_date = `${
        params.statistics_date ? "!<>!" + params.statistics_date : ""
      }`;
       params.channel_id = params.channel_id?'$in'+params.channel_id:''
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>